package com.yxzr.youyou;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.UserInfoBean;
import com.yxzr.youyou.fragment.ExchangeRecordFragment;
import com.yxzr.youyou.fragment.IntegralMallFragment;

import java.util.ArrayList;

public class IntegralMallActivity extends BaseActivity {
    private SegmentTabLayout segmentTabLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"积分商城", "兑换记录"};
    private UserInfoBean.DataBean userInfoBean;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integralmall);
        setTitleWithBack();
        initView();
    }

    private void initView() {
        segmentTabLayout = findViewById(R.id.tl_1);
        viewPager = findViewById(R.id.viewpager);

        mFragments.clear();
        mFragments.add(new IntegralMallFragment((UserInfoBean.DataBean) getIntent().getSerializableExtra("userinfo")));
        mFragments.add(new ExchangeRecordFragment());

        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        segmentTabLayout.setTabData(mTitles);
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                segmentTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(0);
    }

    class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
